<?php namespace models;
/**
 * This class is property of Piotr Poradzisz. All rights reserved. DO NOT COPY!
 * @author Piotr Poradzisz
 * email: piotr.poradzisz@gmail.com
 * tel.: +48668027020
 */
class Relation extends Field{

    #
    private $relation;

    public $name;

    public $tableName;

    public $referenceTable;
    #endregion
    public function  __construct($menu) {
        $mysql = new Mysql();
        $query = "SELECT tableName FROM menu WHERE id = $menu";
        $this->from = $mysql->getRecord($query);

    }

    private function setParams($fieldId) {
        $this->setFieldProperties($fieldId, array('relation'));
        switch ($this->relation) {
            case 5:
            case 3: $properies[] = 'referenceTable';
            case 4:     
            case 2: $properies[] = 'tableName';
            case 1: $properies[] = 'name';
                    break;
        }
        $this->setFieldProperties($fieldId, $properies);
    }

    public function select($fields){
        foreach($fields as $fieldKey => $field){
            if($field['relation'] == 1){
                if($field['name'] == 'id') $select[] = "$this->from.$field[name]";
                else $select[] = "$this->from.$field[name] as '$field[fieldId]'";
            }
            //else if($field['relation'] == 2){} // jeszcze nie ma
            //else if($field['relation'] == 3){} // jeszcze nie ma
            else if($field['relation'] == 4){
                if($value['tableName'] == $this->from) $select[] = "CONCAT_WS(' -> ',t1.name, $this->from.name) as '$field[fieldId]'";
                else $select[] = "$field[tableName].$field[name] as '$field[fieldId]'";
            }
            else if($field['relation'] == 5){} // jeszcze nie ma
        }
        $_SESSION['query']['select'] = implode(',', array_unique($select));
    }

    public function join($fields)
    {
        foreach($fields as $fieldKey => $field)
        {
            if($field['relation'] == 4)
            {
                if($field['tableName'] == $this->from)
                {
                   $join[] = "LEFT JOIN $field[tableName] t1 ON $this->from.parent = t1.id"; 
                }
                else
                {
                    $join[] = "LEFT JOIN $field[tableName] ON $this->from.$field[tableName] = $field[tableName].id";
                }
            }
        }
        if($join) 
        {
            $join = implode(' ', array_unique($join));
            $_SESSION['query']['join'] = $join;            
        }
    }

    public function where($post){
        foreach($post as $fieldId => $value){
            if($value !== null AND $value != ''){
                $this->setParams($fieldId);
                if($this->relation == 1) $where[]  = "$this->from.$this->name LIKE '%$value%'";
                //else if($this->relation == 2);
                //else if($this->relation == 3) $where[]  = "$this->tableName.parent = $value";
                else if($this->relation == 4) $where[] = "$this->tableName.id = $value";
                //else if($this->relation == 5);
            }

        }
        $where = implode(' AND ', array_unique($where));
        if($where) $_SESSION['query']['where']['string'] = "WHERE $where";
        else unset($_SESSION['query']['where']);
    }

}
?>